package com.maserb.monitor.service.author_manage.impl;

import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolOrg.SchoolUserOrgEntity;
import com.maserb.monitor.repository.UserRepository;
import com.maserb.monitor.repository.schoolOrg.SchoolUserOrgRepository;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends JpaSpecificationServiceImpl<UserRepository,
        UserRepository, UserEntity, String>
        implements UserService {

    //班主任角色id
    @Value("${role.class-master.id}")
    private String masterRoleId;

    @Autowired
    private SchoolUserOrgRepository schoolUserOrgRepository;

    @Override
    public Page<UserEntity> findAll(Pageable pageable,String clazzId,String userName, String realName,String orgId) {
        return this.findAll(getSpecification(clazzId,userName,realName,orgId), pageable);
    }

    private Specification<UserEntity> getSpecification(String clazzId,final String userName, final String realName, final String orgId){
        return new Specification<UserEntity>()  {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathUserName = root.get("userName");
                Path<String> pathName = root.get("realName");
                Path<String> pathOrgId = root.get("orgId");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(userName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathUserName, "%" + userName + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(realName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName, "%" + realName + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(orgId)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathOrgId, orgId)));
                }
                if(StringUtils.isEmptyOrWhitespace(orgId)&&!StringUtils.isEmptyOrWhitespace(clazzId)){
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathOrgId,clazzId)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }

    @Override
    public List<UserEntity> findAll(String clazzId,String userName, String realName, String orgId) {
        return new ArrayList( this.findAll(getSpecification(clazzId,userName,realName,orgId),new PageRequest(0,1000)).getContent());
    }

    @Override
    public List<UserEntity> findByOrgId(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity) session.getAttribute("user");
        String orgId = user.getOrgId();
        return this.repository.findByOrgId(orgId);
    }

    @Override
    public UserEntity findClazzMaster(String clazzId) {

        List<UserEntity> userList = schoolUserOrgRepository.findUserByOrgAndRole(clazzId, masterRoleId);
        if(userList != null && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public UserEntity findByUserName(String userName) {
        return repository.findByUserName(userName);
    }

    @Override
    public UserEntity findFirstByName(String name) {
        return repository.findFirstByRealName(name);
    }

    @Override
    public void doReset(String id) {
        UserEntity entity = this.findOne(id);
        entity.setPassword("123456");
        this.save(entity);
    }
}